from app import db
from datetime import datetime

class Product(db.Model):
    """产品表"""
    __tablename__ = 'product'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    code = db.Column(db.String(50), unique=True, nullable=False, comment='产品编码')
    name = db.Column(db.String(100), nullable=False, comment='产品名称')
    specification = db.Column(db.String(200), comment='规格型号')
    unit = db.Column(db.String(20), comment='单位')
    category = db.Column(db.String(50), comment='产品分类')
    description = db.Column(db.Text, comment='产品描述')
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    is_deleted = db.Column(db.Boolean, default=False)
    
    def to_dict(self):
        return {
            'id': self.id,
            'code': self.code,
            'name': self.name,
            'specification': self.specification,
            'unit': self.unit,
            'category': self.category,
            'description': self.description,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'is_deleted': self.is_deleted
        }

class Bom(db.Model):
    """物料清单表"""
    __tablename__ = 'bom'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    product_id = db.Column(db.BigInteger, db.ForeignKey('product.id'), nullable=False, comment='产品ID')
    material_id = db.Column(db.BigInteger, db.ForeignKey('product.id'), nullable=False, comment='物料ID')
    quantity = db.Column(db.Numeric(15, 6), nullable=False, comment='用量')
    unit = db.Column(db.String(20), comment='单位')
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    is_deleted = db.Column(db.Boolean, default=False)
    
    # 关联产品
    product = db.relationship('Product', foreign_keys=[product_id], backref='boms_as_product')
    material = db.relationship('Product', foreign_keys=[material_id], backref='boms_as_material')
    
    def to_dict(self):
        return {
            'id': self.id,
            'product_id': self.product_id,
            'material_id': self.material_id,
            'quantity': float(self.quantity) if self.quantity else 0.0,
            'unit': self.unit,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'is_deleted': self.is_deleted,
            'product': self.product.to_dict() if self.product else None,
            'material': self.material.to_dict() if self.material else None
        }

class ProductionOrder(db.Model):
    """生产订单表"""
    __tablename__ = 'production_order'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    order_no = db.Column(db.String(50), unique=True, nullable=False, comment='生产订单号')
    product_id = db.Column(db.BigInteger, db.ForeignKey('product.id'), nullable=False, comment='产品ID')
    quantity = db.Column(db.Numeric(15, 6), nullable=False, comment='生产数量')
    unit = db.Column(db.String(20), comment='单位')
    planned_start_date = db.Column(db.Date, comment='计划开始日期')
    planned_end_date = db.Column(db.Date, comment='计划结束日期')
    actual_start_date = db.Column(db.DateTime, comment='实际开始时间')
    actual_end_date = db.Column(db.DateTime, comment='实际结束时间')
    status = db.Column(db.Enum('DRAFT', 'APPROVED', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED'), 
                       default='DRAFT', comment='状态')
    created_by = db.Column(db.BigInteger, nullable=False, comment='创建人')
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    is_deleted = db.Column(db.Boolean, default=False)
    
    # 关联产品
    product = db.relationship('Product', backref='production_orders')
    
    def to_dict(self):
        return {
            'id': self.id,
            'order_no': self.order_no,
            'product_id': self.product_id,
            'quantity': float(self.quantity) if self.quantity else 0.0,
            'unit': self.unit,
            'planned_start_date': self.planned_start_date.isoformat() if self.planned_start_date else None,
            'planned_end_date': self.planned_end_date.isoformat() if self.planned_end_date else None,
            'actual_start_date': self.actual_start_date.isoformat() if self.actual_start_date else None,
            'actual_end_date': self.actual_end_date.isoformat() if self.actual_end_date else None,
            'status': self.status,
            'created_by': self.created_by,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'is_deleted': self.is_deleted,
            'product': self.product.to_dict() if self.product else None
        }